 
//create js lib
(function(){
 
	document.writeln('<script type="text/javascript" src="/ad/html/js/jquery-1.6.2.js"></script> ');
	document.writeln('<script type="text/javascript" src="/ad/html/js/dateFormat.js"></script>');
	
	document.writeln('<script type="text/javascript" src="/ad/html/js/querystring.js"></script>');
	/*Ext 4
	document.writeln('<link rel="stylesheet" type="text/css" href="/ad/html/js/ext4/resources/css/ext-all-gray.css"></link>');	
	document.writeln('<script type="text/javascript" src="/ad/html/js/ext4/ext-all.js"></script>');
	*/
	/*Ext 3*/ 
	document.writeln('<link rel="stylesheet" type="text/css" href="/ad/html/js/ext/resources/css/ext-all.css"></link>');
	document.writeln('<link rel="stylesheet" type="text/css"  href="/ad/html/js/ext/resources/css/xtheme-gray.css"><link>')
	
	document.writeln('<script type="text/javascript" src="/ad/html/js/ext/adapter/ext/ext-base.js"></script>');
	document.writeln('<script type="text/javascript" src="/ad/html/js/ext/ext-all-debug.js"></script>') ;
	//document.writeln('<script type="text/javascript" charset="utf-8" src="/ad/html/js/validate.js"></script>') ;
	//document.writeln('<script type="text/javascript" charset="utf-8" src="/ad/html/js/jquery-ui-1.8.13.custom.min.js"></script>'); 
	//document.writeln('<script type="text/javaScript" src="/ad/html/js/jquery.chili-2.2.js"></script>'); 
	//document.writeln('<script type="text/javaScript" src="/ad/html/js/recipes.js"></script>'); 
	//document.writeln('<script type="text/javascript" src="/ad/html/js/twzipcode-1.4-min.js"></script>'); 
	document.writeln('<style type="text/css"> .x-window-dlg .ext-mb-download {background:transparent url(/ad/html/images/large-loading.gif) no-repeat top left;height:46px;</style>'); 
	//document.writeln('<link type="text/css" href="/ad/html/css/validate.css" rel="stylesheet" />'); 
	//document.writeln('<link media="screen" rel="stylesheet" href="/ad/html/css/colorbox.css" />'); 
	
}());



String.prototype.isUrl = function () {
   var strRegex = "^((https|http|ftp|rtsp|mms)?://)";/*
				+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"  
				+ "(([0-9]{1,3}\.){3}[0-9]{1,3}"  
				+ "|"  
				+ "([0-9a-z_!~*'()-]+\.)*"  
				+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." 
				+ "[a-z]{2,6})"  
				+ "(:[0-9]{1,4})?"  
				+ "((/?)|"  
				+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";*/ 
	var re = new RegExp(strRegex); 
	return re.test(this); 
}

String.prototype.formatNumber = function () {
	/*
    if(isNaN(this)) {
            return '';
    }
    if(this == 0) {
        return this;
    }
    var re=/^(-?)(\d+)/,x,y;
     
    if(x=re.exec(this)){
           for (var a=x[2].split(''),i=a.length-3;i>0;i-=3) {a.splice(i,0,',');};
           this=this.replace(re,x[1]+a.join(''));
    } 
    */
    return this;
}

/**
 * 是否為Email格式
 * @returns {Boolean}
 */
String.prototype.isEmail = function() {
  var strr;
  var mail = this.replace("-","");
  
  var re = /(\w+@\w+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i;
  re.exec(mail);
  if(RegExp.$3!="" && RegExp.$3!="." && RegExp.$2!=".")
		strr = RegExp.$1+RegExp.$2+RegExp.$3;
  else
	if(RegExp.$2!="" && RegExp.$2!=".")
		strr = RegExp.$1+RegExp.$2;
	else
		strr = RegExp.$1;
	
	return (strr==mail);
}

 

String.prototype.toJSON = function  () {
	return Ext.util.JSON.decode(decodeURI(this));
}

String.prototype.isPhone = function () {
	var re=  /^[\d\(\)\+\-#\*]*\d[\d\(\)\+\-#\*]*$/;
	return re.test(this.trim());
}

String.prototype.isDate = function () {
	var reg = /^(\d{4})([\/,-])(\d{1,2})\2(\d{1,2})$/;
	var r = this.match(reg);
	if(r==null) return false;
	var d= new Date(r[1], r[3]-1,r[4]);
	var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate();
	date=r[1]+r[2]+((r[3]-1)+1)+r[2]+((r[4]-1)+1);
	return newStr==date;
}

String.prototype.isMobilePhone = function(){
	if (this.trim().search(/^[0-9 | -]*$/)==-1) {///^((13|15|18)\d{9})$/
		return false;
	} else {
		return true;
	}
}
String.prototype.ltrim = function() {
	return this.replace(/^\s*/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s*$/,"");
}

String.prototype.trim  = function() {
	return this.ltrim().rtrim();
}


String.prototype.toDateString = function (pattern){
	return dateFormat(this.toString(), (!pattern) ? "yyyy/mm/dd" : pattern);
}

String.prototype.replaceAll  = function(s1,s2){   
	return this.replace(new RegExp(s1,"gm"),s2);   
} 

Date.prototype.string = function (pattern){ 
	return dateFormat(this, (!pattern) ? "yyyy/mm/dd" : pattern);	
}


function toString(o){
    var parse = function(_o){
    
        var a = [], t;
        
        for(var p in _o){
        
            if(_o.hasOwnProperty(p)){
            
                t = _o[p];
                
                if(t && typeof t == "object"){
                
                    a[a.length]= "\""+p + "\":{ " + arguments.callee(t).join(", ") + "}";
                    
                }
                else {
                    if (t == null) {
                    	   a[a.length] = [ "\""+p+ "\": \"" + null + "\"" ];
                    } else if(typeof t == "string"){
                    
                        a[a.length] = [ "\""+p+ "\": \"" + t.toString() + "\"" ];
                    }
                    else{
                        a[a.length] = [ "\""+p+ "\": " + t.toString()];
                    }
                    
                }
            }
        }
        
        return a;
        
    }
    return "{" + parse(o).join(", ") + "}";
}


function createMask(msg) {
	var mask = new Ext.LoadMask(document.body, {autoShow : true , msg : msg ? msg : "Loading please wait...."});
	return mask;
}



/**
 * @param {dom id} tagId
 * @param {josn array} items
 * @param {value} targetValue
 * @param {text} taggetText
 */
function addOptions(tagId, items, targetValue, taggetText) {
	var select = $("#"+tagId);
	for (var i =0; i < items.length ;i++) {
		select.append('<option value="'+items[i][targetValue]+'">'+items[i][taggetText]+'</option>');
	}
}

function setOptions(tagId , val) {
	var options = Ext.getDom(tagId).options;
	for (var i =0; i < options.length ;i++) {
		 if (options[i].value == val) {
		 	options[i].selected = true;
		 	return;
		 }
	}
}

function removeOptions(tagId) {
	$('#'+tagId).find('option').remove().end();
}

function getRedioValue(tagName) {
	var dom = Ext.query("input[name="+tagName+"][type=radio]");
	
	if (!dom) return "";
	
	for (var i=0; i < dom.length;i++) {
		if (dom[i].checked) {
			return dom[i].value;
		}
	}
	return "";
	
}

function getOptionValue(tagId){
	return getSelectValue(Ext.getDom(tagId).options);
}

function getOptionText(tagId) {
	return getSelectText(Ext.getDom(tagId).options);
}

function getSelectValue(options) {
	for (var i =0 ; i< options.length; i++){
		if (options[i].selected) {
			return options[i].value;
		}
	}
}

function getSelectText(options) {
	for (var i =0 ; i< options.length; i++){
		if (options[i].selected) {
			return options[i].text;
		}
	}
}

/**
 * commit img
 * @param {string} floder name 
 * @return {string} commit img path
 */
function uploadImg(floder, h ,w){
 
	var result  = null;// window.showModalDialog('/ad/upload/UploadForm.php?floder='+ floder ,null,'dialogHeight:'+h+'pt;dialogWidth:'+w+'pt;center:yes;status=no,menubar=no');  
	result = window.showModalDialog('/ad/upload/UploadForm.php?floder='+ floder,null,'dialogHeight:'+h+'pt;dialogWidth:'+w+'pt;center:yes;status=no,menubar=no,location=no');
	//window.open ('/ad/upload/UploadForm.php?floder='+ floder , "newWidnw", "height="+h+", width="+ w +",alwaysRaise=1, z-look=1,top=0, left=0,toolbar=no, menubar=no, scrollbars=no, resizable=yes, location=no, status=no")//写成一行 
	 
	if (result ==  null) {
		return null;
	} else {
		return "/ad/upload/"+result;
	} 
}
 
function waitShow(msg) {
	  Ext.MessageBox.show({
           title: 'Please wait',
           progressText: 'Initializing...',
           width:300,
           progress:true,
           msg:  (!msg ? 'Loading...' : msg),
           progressText: (!msg ? 'Loading...' : msg),
           wait:true,
           icon:'ext-mb-download'  
       });
}



function waitHide() {
	Ext.MessageBox.hide();
}

String.prototype.isNull = function(){
	if (this == null || this.trim()== "") {
		return true;
	} else {
		return false;
	}
}

function Hash()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
    this.toArray = function () {
    	return this.items;
    }
    this.getSize = function () {
    	return this.length;
    }
	this.remove = function(in_key)
	{
		var tmp_previous;
		if (typeof(this.items[in_key]) != 'undefined') {
			this.length--;
			var tmp_previous = this.items[in_key];
			delete this.items[in_key];
		}
	   
		return tmp_previous;
	}

	this.get = function(in_key) {
		return this.items[in_key];
	}

	this.set = function(in_key, in_value)
	{
		var tmp_previous;
		if (typeof(in_value) != 'undefined') {
			if (typeof(this.items[in_key]) == 'undefined') {
				this.length++;
			}
			else {
				tmp_previous = this.items[in_key];
			}

			this.items[in_key] = in_value;
		}
	   
		return tmp_previous;
	}

	this.hasItem = function(in_key)
	{
		return typeof(this.items[in_key]) != 'undefined';
	}

	this.clear = function()
	{
		for (var i in this.items) {
			delete this.items[i];
		}

		this.length = 0;
	}
}

function openWindow(url , width, height, property) {
    var left = (screen.width/2) - width/2;
    var top = (screen.height/2) - height/2;
   
    if (property == null) {
        property = 'toolbar=no,location=no,status=no,menubar=yes,scrollbars=no,scrollbars=yes,resizable=yes, width=' + width + ', height='+height +',left='+left+',top='+top+',screenX='+left+',screenY='+top;		
	}
	
	var obj = window.open(url ,"openWin", property);
	return obj;
}


function getBrowser() {
	 var sAgent = navigator.userAgent.toLowerCase();
	  var obj = {};
		 obj.isIE = (sAgent.indexOf("msie")!=-1); //IE6.0-7
		 obj.isFF = (sAgent.indexOf("firefox")!=-1);//firefox
		 obj.isSa = (sAgent.indexOf("safari")!=-1);//safari
		 obj.isOp = (sAgent.indexOf("opera")!=-1);//opera
		 obj.isNN = (sAgent.indexOf("netscape")!=-1);//netscape
		 obj.isMa = this.isIE;//marthon
		 obj.isOther = (!sAgent.isIE && !sAgent.isFF && !sAgent.isSa && !sAgent.isOp && !sAgent.isNN && !sAgent.isSa);//unknown Browser
		 return obj;
}
